// 0 引入 用来发送请求的 方法 一定要把路径补全
import { request } from "../../request/index.js";
import { sendServer,sendServerGet } from '../../request/common.js'
import { getGoodsType,getGoodsList} from '../../request/module.js'
Page({ 
  data: {
    // 破不留
    products: [],
    lHeight:0,
    rHeight:0,
    lList:[],
    rList:[],
    //瀑布流
    // tempfilePath:'http://localhost:8080/img/type/',
    tempfilePath:'http://192.168.0.103:8080/img/type/',
    // tempfilePath1:'http://localhost:8080/img/',
    tempfilePath1:'http://192.168.0.103:8080/img/',
    // 导航 数组
    catesList:[],
    // 楼层数据
    floorList:[],
    // 轮播图数组
    swiperList1: [
      {
        image_src:'swiper/b1.jpg'
      },
      {
        image_src:'swiper/b2.jpg'
      }, 
      {
        image_src:'swiper/b3.jpg'
      },

    ],
    catesList1:[],
    floorList:[],
    defImg:'../../images/def.jpg'
  },
  // 页面开始加载 就会触发
  onLoad: function (options) {
    // 1 发送异步请求获取轮播图数据  优化的手段可以通过es6的 promise来解决这个问题    
    //分类
    this.getCateList();
    //楼层
    this.getFloorList();
    
      
  },
  getCateList(){
    getGoodsType().then((res)=>{
      console.log(res)
      let a=res.extend.list.slice(0,4)
			let b=["fen.jpg","tuanshan.jpg","peishi.png","nan.jpg",]
			for(let i=0;i<a.length;i++){
				a[i].img=b[i]
      }
      this.setData({
        catesList1:a
      });
    })
  },
  showGoodsDetail(options){
    let goodsId = options.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/goods_detail/index?id='+goodsId,
    })
  },
  // 获取 楼层数据
  getFloorList(){
    getGoodsList().then((res)=>{
      console.log(res)
      let list=res.extend.goodsList
      let proList=list.slice(11,71);
      proList.forEach(item=>{
        item.show=true;
      })
      this.setData({
        miaoList:list.slice(0,10),
        products:proList
      })
      // this.showImg()


      //根据goodstypeId进行分组 
      // let map = {}
      // let arr =list
      // for (let i = 0; i < arr.length; i++) {
      //     let ai = arr[i]
      //     if (!map[ai.goodstypeId]) {
      //         map[ai.goodstypeId] = [ai]
      //     } else {
      //         map[ai.goodstypeId].push(ai)
      //     }
      // }
      // let listData = []
      // Object.keys(map).forEach(key => {
      //   listData.push({
      //       goodstypeId: key,
      //       data: map[key].slice(0,5),
      //     })
      // })
      // console.log(listData)
      // listData.forEach(item=>{
      //   let catesList=this.data.catesList1
      //   for(let i=0;i<catesList.length;i++){
      //     if(item.goodstypeId==catesList[i].id){
      //       item.goodstypename=catesList[i].goodstypename
      //     }
      //   }
      // })
      // this.setData({
      //   floorList:listData
      // })
    })
  },

  imgLoad:function(e){
    var lList = this.data.lList;
    var rList = this.data.rList;
    var lHeight = this.data.lHeight;
    var rHeight = this.data.rHeight;
    if (lHeight == rHeight || rHeight > lHeight){
      lList.push(e.currentTarget.dataset.product);
      lHeight += e.detail.height / e.detail.width;
      this.setData({
        lList,
        lHeight
      })
      // this.showImg()
    }else{
      rList.push(e.currentTarget.dataset.product);
      rHeight += e.detail.height / e.detail.width;
      this.setData({
        rList,
        rHeight
      })
      // this.showImg()
    }
  },
  showMiaoshao(options){
    let goodsId = options.currentTarget.dataset.id;
    console.log(goodsId)
    wx.navigateTo({
      url: '/pages/goods_detail/index?id='+goodsId
    })
  },
  showImg(){
    let lList=this.data.lList
    for (let i in this.data.lList){
      wx.createIntersectionObserver().relativeToViewport({bottom: 20}).observe('.item-'+ i, (ret) => {
        if (ret.intersectionRatio > 0){
          lList[i].show = true 
        }
        this.setData({ // 更新数据
          lList
        })
      })
    }

    let rList=this.data.rList
    for (let i in this.data.rList){
      wx.createIntersectionObserver().relativeToViewport().observe('.item-'+ i, (ret) => {
        if (ret.intersectionRatio > 0){
          rList[i].show = true 
        }
        this.setData({ // 更新数据
          rList
        })
      })
      
    }
    console.log(rList)

    // let products = this.data.products  // 获取原数据
    
    // console.log(products)
    // for (let i in this.data.products){
    //   wx.createIntersectionObserver().relativeToViewport({bottom: 20}).observe('.item-'+ i, (ret) => {
    //     if (ret.intersectionRatio > 0){
    //       products[i].show = true 
    //     }
    //     this.setData({ // 更新数据
    //       products
    //     })
    //   })
    // }
  }
  
})
